﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ARA.Domain.Services
{
    public class AnalyseService
    {
        public List<Datas.Person> AnalyseByDate(Guid batchId, DateTime date)
        {
            DateTime dtBegin = new DateTime(date.Year, date.Month, date.Day, 0, 0, 0);
            DateTime dtEnd = new DateTime(date.Year, date.Month, date.Day, 23, 59, 59);

            return AnalyseByDateRange(batchId, dtBegin, dtEnd);
        }

        public List<Datas.Person> AnalyseByDateRange(Guid batchId, DateTime beginDate, DateTime endDate)
        {
            List<Entities.Record> records = new QueryService().QueryRecordList(batchId, beginDate, endDate);
            List<Datas.Person> personList = new List<Datas.Person>();

            for (int index = 0; index < records.Count; index++)
            {
                Entities.Record record = records[index];
                Datas.Person person = null;

                for (int pIndex = 0; pIndex < personList.Count; pIndex++)
                {
                    if (string.Equals(personList[pIndex].Account, record.Account))
                    {
                        person = personList[pIndex];
                        break;
                    }
                }
                if (person == null)
                {
                    person = new Datas.Person(records[index].Account, records[index].Name);
                    personList.Add(person);
                }
                person.AddSegment(record.LoginTime, record.LogoutTime);
            }

            return personList;
        }

        public List<Datas.Person> AnalyseByDateRange(Guid batchId, Guid departmentId, DateTime beginDate, DateTime endDate)
        {
            List<Entities.Record> records = new QueryService().QueryRecordList(batchId, departmentId, beginDate, endDate);
            List<Datas.Person> personList = new List<Datas.Person>();

            for (int index = 0; index < records.Count; index++)
            {
                Entities.Record record = records[index];
                Datas.Person person = null;

                for (int pIndex = 0; pIndex < personList.Count; pIndex++)
                {
                    if (string.Equals(personList[pIndex].Account, record.Account))
                    {
                        person = personList[pIndex];
                        break;
                    }
                }
                if (person == null)
                {
                    person = new Datas.Person(records[index].Account, records[index].Name);
                    personList.Add(person);
                }
                person.AddSegment(record.LoginTime, record.LogoutTime);
            }

            return personList;
        }

        public List<Datas.Person> AnalyseByPerson(Guid batchId, string name, DateTime beginDate, DateTime endDate)
        {
            List<Entities.Record> records = new QueryService().QueryRecordList(batchId, name, beginDate, endDate);
            List<Datas.Person> personList = new List<Datas.Person>();

            for (int index = 0; index < records.Count; index++)
            {
                Entities.Record record = records[index];
                Datas.Person person = null;

                for (int pIndex = 0; pIndex < personList.Count; pIndex++)
                {
                    if (string.Equals(personList[pIndex].Account, record.Account))
                    {
                        person = personList[pIndex];
                        break;
                    }
                }
                if (person == null)
                {
                    person = new Datas.Person(records[index].Account, records[index].Name);
                    personList.Add(person);
                }
                person.AddSegment(record.LoginTime, record.LogoutTime);
            }

            return personList;
        }
    }
}
